Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with*generating a*variable in STATA

    Hi, I have a dataset which has a variable mother_no that stores the number that represents mother in the household roster. Another set of variables h12_1, h12_2, h12_3......etc represent the age of the household member 1, 2 and 3 respectively. I want to now create a variable mother_age that stores the mother's age.

    mother_no h12_1 h12_2 h12_3 h12_4 h12_5 mother_age
    3 23 34 23 54 56 23
    1 24 54 43 76 23 24
    3 54 73 35 38 49 73
    4 12 13 46 37 42 37
    5 52 73 74 24 14 14


    The above table shows an example. For the first row, since mother_no takes a value 3, mother_age stores the value of h12_3.

    How do I create the variable mother_age for my data. Thanks.

  • #2
    Please note https://www.statalist.org/forums/help#stata and https://www.statalist.org/forums/help#spelling

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(mother_no h12_1 h12_2 h12_3 h12_4 h12_5 mother_age)
    3 23 34 23 54 56 23
    1 24 54 43 76 23 24
    3 54 73 35 38 49 73
    4 12 13 46 37 42 37
    5 52 73 74 24 14 14
    end
    
    gen wanted = . 
    
    forval j = 1/5 {
        replace wanted = h12_`j' if mother_no == `j'
    }

    Comment

    Working...
    X